package com.company.cloud.acct.modules.mgr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.acct.modules.app.service.AcctCashService;
import com.company.cloud.acct.modules.base.entity.AccountCashInfo;
import com.company.cloud.acct.modules.base.entity.BankCardInfo;
import com.company.cloud.acct.modules.base.service.AccountCashInfoService;
import com.company.cloud.acct.modules.base.service.BankCardInfoService;
import com.company.cloud.acct.modules.cache.models.AcctCashModel;
import com.company.cloud.acct.modules.cache.models.BankCardModel;
import com.company.cloud.acct.modules.cache.models.CashConfigModel;
import com.company.cloud.acct.modules.cache.service.AcctCacheService;
import com.company.cloud.acct.modules.cache.utils.AcctConvertUtils;
import com.company.cloud.acct.modules.mgr.controller.req.AcctCashQueryParam;
import com.company.cloud.acct.modules.mgr.controller.req.BankCardQueryParam;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.user.modules.api.models.UserModel;
import com.company.cloud.user.modules.api.service.UserPlatformRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;
import java.util.function.Function;

@Authc
@RestController
@RequestMapping("/mgr/acct/cash")
@Api(tags="Acct_MGR_账户提现管理")
public class AcctCashMgrController {

    @Autowired
    private AccountCashInfoService accountCashInfoService;
    @Autowired
    private AcctCacheService acctCacheService;
    @Autowired
    private UserPlatformRemoteService userPlatformRemoteService;

    @PostMapping("/query")
    @ApiOperation(value="查询账户提现记录列表",response = AcctCashModel.class)
    public BaseResult query(@RequestBody AcctCashQueryParam params) {
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
//        Set<Integer> userIds = brokerRemoteService.getUserIdsByName(params.getUserName()).getData();
        IPage<AccountCashInfo> page = accountCashInfoService.page(new Page<>(params.getPage(), params.getRows()), new QueryWrapper<AccountCashInfo>()
                .eq("company_no", userInfo.getCompanyNo())
                .eq(CheckUtils.hasInt(params.getUserId()), "user_id", params.getUserId())
                .eq(CheckUtils.hasText(params.getType()),"type", params.getType())
//                .in(CheckUtils.hasText(params.getUserName()), "user_id", userIds)
                .eq("state", params.getState())
        );
        IPage<AcctCashModel> data = page.convert(new Function<AccountCashInfo, AcctCashModel>() {
            @Override
            public AcctCashModel apply(AccountCashInfo entity) {
                return AcctConvertUtils.getAcctCashModel(entity, null);
            }
        });
        return BaseResult.ok();
    }
    @PostMapping("/queryInfo")
    @ApiOperation(value="查询账户提现记录列表",response = AcctCashModel.class)
    public BaseResult queryInfo(@RequestBody AcctCashQueryParam params) {
        IPage<AccountCashInfo> page = accountCashInfoService.page(new Page<>(params.getPage(), params.getRows()), new QueryWrapper<AccountCashInfo>()
                        .eq(CheckUtils.hasInt(params.getUserId()), "user_id", params.getUserId())
                        .eq(CheckUtils.hasText(params.getType()),"type", params.getType())
                        .eq(CheckUtils.hasText(params.getState()),"state", params.getState())
                        .orderByDesc("cdt ")
        );
        IPage<AcctCashModel> data = page.convert(new Function<AccountCashInfo, AcctCashModel>() {
            @Override
            public AcctCashModel apply(AccountCashInfo entity) {
                AcctCashModel acctCashModel = AcctConvertUtils.getAcctCashModel(entity, null);
                BaseClientResult<UserModel> user = userPlatformRemoteService.getUser(entity.getUserId());
                if(user.getData()!=null){
                    UserModel userModel= user.getData();
                    acctCashModel.setUserName(userModel.getNickName());
                    acctCashModel.setMobile(userModel.getMobile());
                    acctCashModel.setHeadImg(userModel.getHeadImg());
                }
                return acctCashModel;
            }
        });
        return BaseResult.ok(data);
    }

    @GetMapping("/get")
    @ApiOperation(value="查询账户提现记录详情",response = AcctCashModel.class)
    public BaseResult get(@RequestParam("acctId") Integer acctId) {
        AccountCashInfo entity = accountCashInfoService.getById(acctId);
        if (null == entity) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        Map<Integer, BankCardModel> cardMap = acctCacheService.getBankCardMapByUser(entity.getUserId());
        return BaseResult.ok(AcctConvertUtils.getAcctCashModel(entity, cardMap));
    }
}
